//自定义验证

function emailCheck(email) {
    var emailreg = /^([a-zA-Z0-9]+[_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!emailreg.test(email)) {
        return false;
    }
    return true;
}


// 手机号码星号出来
function asteriskMobile(mobile) {
    if (mobile) {
        var str = mobile.toString().replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
        return str;
    }
}

function pwdCheck(pwd) {
    var pwdreg = /^(?!([a-zA-Z]+|\d+)$)[a-zA-Z\d]{6,18}$/;
    if (!pwdreg.test(pwd)) {
        return false;
    }
    return true;
}

function phoneNumCheck(phoneNUm) {
    var phoneNumreg = /^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$/;
    if (!phoneNumreg.test(phoneNUm)) {
        return false;
    }
    return true;
}

function stringCheck(string) {
    var result = removeSpace(string);
    if (result.length === 0) {
        return false;
    }
    return true;
}

function removeSpace(string) {
    var result;
    result = string.trim();
    result = result.replace(/\s/g, '');
    return result;
}

function formatDate(date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}

function padLeftZero(str) {
  return ('00' + str).substr(str.length);
}

function dateFormat(time) {
    var date = new Date(parseInt(time));
    var tempMonth = date.getMonth() + 1;
    var year = date.getFullYear();
    var month = tempMonth >= 10 ? tempMonth : '0' + tempMonth;
    var day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
    var str = year + '-' + month + '-' + day;
    return str;
}

function formatMsgTime(timespan) {
  var dateTime = new Date(Number(timespan));
  var year = dateTime.getFullYear();
  var month = dateTime.getMonth() + 1;
  var day = dateTime.getDate();
  var hour = dateTime.getHours();
  var minute = dateTime.getMinutes();
  var second = dateTime.getSeconds();
  var now = new Date();
  var now_new = now.getTime(); //typescript转换写法
  var milliseconds = 0;
  var timeSpanStr;
  milliseconds = now_new - timespan;
  if (milliseconds <= 1000 * 60 * 1) {
    timeSpanStr = '刚刚';
  } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
    timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
  } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
    timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
  } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
    timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
  } else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
    timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
  } else {
    timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
  }
  return timeSpanStr;
}


function datetimeFormat(time) {
    var date = new Date(time);
    var tempMonth = date.getMonth() + 1;
    var month = tempMonth >= 10 ? tempMonth : '0' + tempMonth;
    var day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
    var hour = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
    var minutes = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
    var str = `${month}-${day} ${hour}:${minutes}`;
    return str;
}

function timeFormat(time, type) {
    var date = new Date(time);
    var tempMonth = date.getMonth() + 1;
    var year = date.getFullYear();
    var month = tempMonth >= 10 ? tempMonth : '0' + tempMonth;
    var day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();
    var hour = date.getHours() >= 10 ? date.getHours() : '0' + date.getHours();
    var minutes = date.getMinutes() >= 10 ? date.getMinutes() : '0' + date.getMinutes();
    if (type) {
        if (type === 1) {
            var str = `${year}-${month}-${day} ${hour}:${minutes}`;
            return str;
        } else if (type === 2) {
            var str = `${year}-${month}-${day}`;
            return str;
        } else if (type === 3) {
            var str = `${hour}:${minutes}`;
            return str;
        }
    } else {
        var str = `${year}-${month}-${day} ${hour}:${minutes}`;
        return str;
    }

}
function countDownFormat(time) {
    var date = time;
    var minutes = Math.floor(date / 60) >= 10 ? Math.floor(date / 60) : '0' + Math.floor(date / 60);
    var seconds = Math.floor(date % 60) >= 10 ? Math.floor(date % 60) : '0' + Math.floor(date % 60);
    var str = `${minutes}:${seconds}`;
    return str;
}

// 对象排序
function arraySoft(dataList) {
    var newObj = dataList.sort(function(a, b) {
        if (a['name'] > b['name']) {
            return 1
        } else if (a['name'] == b['name']) {
            return 0
        } else {
            return -1
        }
    })
    return newObj
}

//判断浏览器类型
function BrowserType() {
    var flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
    return flag;
}
